﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using agsXMPP.protocol.client;
using System.IO;
using ScriptsAPI;

namespace JBot.Windows {
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class OptionsWindow : Window {
		public OptionsWindow( ) {
			this.InitializeComponent( );
			Properties.Settings.Default.Reload( );
			LoadSettings( );
			// Insert code required on object creation below this point.
		}

		private string[ ] boolean = new string[ ] { "True", "False" };
		private int[ ] status = new int[ ] { 1, 2, 3, 4, 5, 6, 0 };

		#region Zdarzenia okna
		private void B_Reset_Click(object sender, RoutedEventArgs e) {
			Properties.Settings.Default.Reset( );
			LoadSettings( );
			this.DialogResult = true;
		}
		private void B_Apply_Click(object sender, RoutedEventArgs e) {
			if (SaveSettings( )) {
				Properties.Settings.Default.Save( );
				Properties.Settings.Default.Reload( );
				LoadSettings( );
				this.DialogResult = true;
			}
		}
		private void B_OK_Click(object sender, RoutedEventArgs e) {
			if (SaveSettings( )) {
				Properties.Settings.Default.Save( );
				Properties.Settings.Default.Reload( );
				this.DialogResult = true;
				this.Close( );
			} else {
				MessageBox.Show("Warning!", "Changed has been not saved!");
			}
		}
		#endregion

		#region Funkcje i procedury
		private void LoadSettings( ) {
			this.TB_Login.Text = Properties.Settings.Default.Login;
			this.TB_Password.Password = Properties.Settings.Default.Password;
			this.TB_Server.Text = Properties.Settings.Default.Server;
			this.TB_Port.Text = Properties.Settings.Default.Port.ToString( );

			this.CB_SSL.SelectedIndex = Array.IndexOf(boolean, Properties.Settings.Default.UseSSL.ToString( ));
			this.CB_STLS.SelectedIndex = Array.IndexOf(boolean, Properties.Settings.Default.UseStartTLS.ToString( ));
			this.CB_Alive.SelectedIndex = Array.IndexOf(boolean, Properties.Settings.Default.KeepAlive.ToString( ));
			this.TB_AliveInt.Text = Properties.Settings.Default.KeepAliveInterval.ToString( );

			this.TB_NickName.Text = Properties.Settings.Default.BotNickName;
			this.TB_AdminJID.Text = Properties.Settings.Default.JIDAdmin;
			Presence p = new Presence( ) {
				Show = Properties.Settings.Default.PShow,
				Type = Properties.Settings.Default.PType
			};
			this.CB_Status.SelectedIndex = Array.IndexOf(status, JFunctions.getStatusInt(p));
			this.TB_Description.Text = Properties.Settings.Default.Status;

			this.listViewScripts.ItemsSource = new ScriptsList(Properties.Settings.Default.Scripts.Split('\n'));
		}
		private bool CheckSettings( ) {
			return true;
		}
		private bool SaveSettings( ) {
			if (CheckSettings( )) {
				Properties.Settings.Default.Login = this.TB_Login.Text;
				Properties.Settings.Default.Password = this.TB_Password.Password;
				Properties.Settings.Default.Server = this.TB_Server.Text;
				Properties.Settings.Default.Port = Convert.ToInt32(this.TB_Port.Text);

				Properties.Settings.Default.UseSSL = Convert.ToBoolean(boolean[this.CB_SSL.SelectedIndex]);
				Properties.Settings.Default.UseStartTLS = Convert.ToBoolean(boolean[this.CB_STLS.SelectedIndex]);
				Properties.Settings.Default.KeepAlive = Convert.ToBoolean(boolean[this.CB_Alive.SelectedIndex]);
				Properties.Settings.Default.KeepAliveInterval = Convert.ToInt32(this.TB_AliveInt.Text);

				Properties.Settings.Default.BotNickName = this.TB_NickName.Text;
				Properties.Settings.Default.JIDAdmin = this.TB_AdminJID.Text;
				Presence p = JFunctions.getStatusPre(status[this.CB_Status.SelectedIndex]);
				Properties.Settings.Default.PType = p.Type;
				Properties.Settings.Default.PShow = p.Show;
				Properties.Settings.Default.Status = this.TB_Description.Text;
				return true;
			} else {
				return false;
			}
		}
		#endregion

		private void textBox_KeyDown(object sender, KeyEventArgs e) {
			if (e.Key == Key.Enter) {
				this.B_OK_Click(sender, null);
			}
		}
	}

	public class ScriptsList : List<ScriptItem> {
		public ScriptsList( ) {
		}
		public ScriptsList(string[ ] items) {
			foreach (string item in items) {
				this.Add(new ScriptItem(item));
			}
		}
	}
	public class ScriptItem {
		public ScriptItem( ) {
		}
		public ScriptItem(string path) {
			this.Path = path;
			try {
				FileInfo fileInfo = new FileInfo(path);
				this.Name = fileInfo.Name;
			} catch {
			}
		}

		public string Name {
			get;
			set;
		}
		public string Path {
			get;
			set;
		}
	}
}